using LeaRun.Application.Entity.SystemManage;
using LeaRun.Application.IService.SystemManage;
using LeaRun.Data.Repository;
using LeaRun.Util;
using LeaRun.Util.WebControl;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace LeaRun.Application.Service.SystemManage
{
    /// <summary>
    /// DataSouce数据源管理
    /// </summary>
	public class DataSourceService : RepositoryFactory<DataSourceEntity>, DataSourceIService
	{
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="queryJson"></param>
        /// <returns></returns>
		public IEnumerable<DataSourceEntity> GetPageList(Pagination pagination, string queryJson)
		{
			return base.BaseRepository().FindList(pagination);
		}
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="queryJson"></param>
        /// <returns></returns>
		public IEnumerable<DataSourceEntity> GetList(string queryJson)
		{
			return base.BaseRepository().IQueryable().ToList<DataSourceEntity>();
		}
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
		public DataSourceEntity GetEntity(string keyValue)
		{
			return base.BaseRepository().FindEntity(keyValue);
		}
        /// <summary>
        /// 获取Datatable
        /// </summary>
        /// <param name="dataBaseLinkId"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
		public DataTable GetTableDataList(string dataBaseLinkId, string tableName)
		{
			DataBaseLinkEntity entity = new DataBaseLinkService().GetEntity(dataBaseLinkId);
			if (entity != null)
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append(("SELECT * FROM " + tableName) ?? "");
				return base.BaseRepository(entity.DbConnection).FindTable(stringBuilder.ToString());
			}
			return null;
		}
        /// <summary>
        /// 测试数据
        /// </summary>
        /// <param name="dbtype"></param>
        /// <param name="connection"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
		public string TestData(string dbtype, string connection, string sql)
		{
			string result;
			try
			{
				SqlConnection sqlConnection = null;
				if (dbtype == "SqlServer")
				{
					sqlConnection = new SqlConnection(connection);
					string arg_1C_0 = sqlConnection.DataSource;
				}
				DataTable dataTable = new DataTable();
				try
				{
					sqlConnection.Open();
					new SqlDataAdapter(sql, sqlConnection).Fill(dataTable);
					sqlConnection.Close();
					result = dataTable.ToJson();
				}
				catch
				{
					result = "[{\"失败\":\"请检查数据连接或sql语句\"}]";
				}
			}
			catch (Exception)
			{
				result = "[{\"失败\":\"请检查数据连接或sql语句\"}]";
			}
			return result;
		}
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="keyValue"></param>
		public void RemoveForm(string keyValue)
		{
			base.BaseRepository().Delete(keyValue);
		}
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="entity"></param>
		public void SaveForm(string keyValue, DataSourceEntity entity)
		{
			if (!string.IsNullOrEmpty(keyValue))
			{
				entity.Modify(keyValue);
				base.BaseRepository().Update(entity);
				return;
			}
			entity.Create();
			base.BaseRepository().Insert(entity);
		}
	}
}
